angular.module('app').config(function ($stateProvider) {

    $stateProvider.state('app.geolocation', {
        url         : '/geolocation',
        label       : '地理位置',
        controller  : 'app.geolocation',
        controllerAs: '$ctrl',
        templateUrl : 'view/app/geolocation/geolocation.html'
    });
}).controller('app.geolocation', function ($ionicPlatform, $timeout) {
    var ctrl = this, watchID;
    ctrl.geolocations = [];
    /**
     * Position
     * - coords: Coordinates 的一个集合
     * - timestamp:
     */

    /**
     * Coordinates
     * - latitude: (number)十进制度的纬度
     * - longitude: (number)十进制度的经度
     * - altitude: (number)海拔
     * - accuracy: (number)纬度和经度的准确度
     * - altitudeAccuracy: (number)海拔的准确度
     * - heading: (number)方向
     * - speed: (number)速度
     */

    /**
     * PositionError
     * - code [PositionError.PERMISSION_DENIED(code 1), PositionError.POSITION_UNAVAILABLE(code 2), PositionError.TIMEOUT(code 3)]
     * - message
     */
    $ionicPlatform.ready(function () {
        ctrl.get = function () {
            /**
             * navigator.geolocation.getCurrentPosition(geolocationSuccess, [geolocationError], [geolocationOptions]);
             * - geolocationSuccess: 会传入一个 Position 对象的参数
             */
            navigator.geolocation.getCurrentPosition(function (position) {
                // Position, Coordinates, PositionError 是只读的, 所以不能直接使用 ctrl.currentPosition = position, 这样将会获取不到属性.
                $timeout(function () {
                    ctrl.currentPosition = {
                        coords   : {
                            latitude        : position.coords.latitude,
                            longitude       : position.coords.longitude,
                            altitude        : position.coords.altitude,
                            accuracy        : position.coords.accuracy,
                            altitudeAccuracy: position.coords.altitudeAccuracy,
                            heading         : position.coords.heading,
                            speed           : position.coords.speed
                        },
                        timestamp: position.timestamp
                    };
                });
            }, onError);
        };
        ctrl.watch = function () {
            /**
             * var watchID = navigator.geolocation.watchPosition(geolocationSuccess, [geolocationError], [geolocationOptions]);
             *
             * - geolocationOptions
             *   - enableHighAccuracy: 是否启用高精度
             *   - timeout: 在指定的 timeout 时间内未执行 geolocationSuccess 则会执行 geolocationError
             *   - maximumAge: 接收以个不高于指定的 maximumAge 的缓存 position
             */
            if (watchID) {
                return;
            }
            watchID = navigator.geolocation.watchPosition(function (position) {
                $timeout(function () {
                    ctrl.geolocations.unshift({
                        coords   : {
                            latitude        : position.coords.latitude,
                            longitude       : position.coords.longitude,
                            altitude        : position.coords.altitude,
                            accuracy        : position.coords.accuracy,
                            altitudeAccuracy: position.coords.altitudeAccuracy,
                            heading         : position.coords.heading,
                            speed           : position.coords.speed
                        },
                        timestamp: position.timestamp
                    });
                    if (ctrl.geolocations.length > 4) {
                        ctrl.geolocations.length = 3;
                    }
                });
            }, onError, {
                maximumAge        : 3000,
                timeout           : 5000,
                enableHighAccuracy: true
            });
        };
        ctrl.clearWatch = function () {
            /**
             * navigator.geolocation.clearWatch(watchID);
             */
            if (!watchID) {
                return;
            }
            navigator.geolocation.clearWatch(watchID);
            watchID = undefined;
            ctrl.geolocations.length = 0;
        };
    });

    function onError(PositionError) {
        alert('onError - ' + JSON.stringify(PositionError));
    }
});